{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with jobs\n", "\n", "The job class obtained after running a QuantumCircuit has interesting properties that can be used." ] }, { "cell_type": "code", "source": [ "import os\n", "from c12_callisto_clients.user_configs import UserConfigs\n", "from c12_callisto_clients.qiskit.c12sim_provider import C12SimProvider\n", "from qiskit import QuantumCircuit" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2026-12-01T08:24:57.221523Z", "start_time": "2026-12-01T08:24:56.155898Z" } }, "outputs": [], "execution_count": 1 }, { "cell_type": "code", "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2026-12-01T08:24:58.805405Z", "start_time": "2026-12-01T08:24:58.559846Z" } }, "source": [ "\n", "TOKEN = os.getenv(\"C12_TOKEN\")\n", "configs = UserConfigs.model_validate({\"token\" : TOKEN})\n", "c12_simulator_provider = C12SimProvider(configs)\n", "c12_simulator_backend = c12_simulator_provider.get_backend('Callisto Emulator')\n", "circuit = QuantumCircuit(2)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "\n", "circuit.draw()" ], "outputs": [ { "data": { "text/plain": [ " ┌───┐ \n", "q_0: ┤ H ├──■──\n", " └───┘┌─┴─┐\n", "q_1: ─────┤ X ├\n", " └───┘" ], "text/html": [ "
┌───┐ \n",
"q_0: ┤ H ├──■──\n",
" └───┘┌─┴─┐\n",
"q_1: ─────┤ X ├\n",
" └───┘"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Two main methods available for each Job instance are job_id() and status().\n",
"\n",
"1. `job_id()` method returns the unique identifier of a job as a UUID4 string (a universally unique identifier (UUID), version 4, is a 36-character alphanumeric random generated string). This id can be used for later assessment of the job properties.\n",
"\n",
"2. `status()` method is used to get the status of a job execution as an instance of JobStatus class. Available statuses are:\n",
" - `QUEUED` = Job is queued. It waits for execution\n",
" - `RUNNING` = Job is currently running.\n",
" - `CANCELLED` = Job has been canceled.\n",
" - `DONE` = Job has been successfully done.\n",
" - `ERROR` = There has been an error during the execution of a job.\n",
"\n",
"The status of a running job can be changed depending on the current state of job execution.\n"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T08:25:05.233230Z",
"start_time": "2026-12-01T08:25:04.621211Z"
}
},
"source": [
"c12_job = c12_simulator_backend.run(circuit)\n",
"\n",
"print(f\"Job id: {c12_job.job_id()}\") # Get a job UUID\n",
"print(f\"Status: {c12_job.status()}\") # Get a current job status"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Job id: 06f54961-4fcb-4886-aac7-455e13d1e414\n",
"Status: JobStatus.RUNNING\n"
]
}
],
"execution_count": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result of a job execution can be obtained with the `result()` method. This method can have one optional argument (timeout). The `timeout` argument specifies how long the method will wait for the execution of the task. If the time limit is exceeded, the `C12SimJobError` exception is raised. If we do not specify this argument, its default value is None, meaning the method will block until the simulation is finished.\n",
"\n",
"Another way to get the results is to check the job status periodically until it is `DONE` and then call the `result()` function."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T08:26:40.116177Z",
"start_time": "2026-12-01T08:25:11.675159Z"
}
},
"source": [
"from qiskit.providers import JobStatus\n",
"import time\n",
"job_final_states = [JobStatus.DONE, JobStatus.ERROR, JobStatus.CANCELLED]\n",
"c12_job_id = c12_job.job_id()\n",
"while True:\n",
" job_status = c12_job.status()\n",
" print(f'{c12_job_id}: {job_status}')\n",
" if job_status in job_final_states:\n",
" break\n",
" time.sleep(5) # Wait 5 s\n",
"\n",
"if c12_job.status() == JobStatus.DONE:\n",
" c12_result = c12_job.result()\n",
" print(c12_result.get_counts())"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.RUNNING\n",
"06f54961-4fcb-4886-aac7-455e13d1e414: JobStatus.DONE\n",
"{'00': 494, '01': 2, '10': 1, '11': 527}\n"
]
}
],
"execution_count": 4
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T07:47:37.710573Z",
"start_time": "2026-12-01T07:46:03.528080Z"
}
},
"source": [
"# Running a job that will block until finished\n",
"c12_job = c12_simulator_backend.run(circuit)\n",
"c12_result = c12_job.result()\n",
"if c12_job.status() == JobStatus.DONE:\n",
" print(c12_result.get_counts())"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'00': 507, '10': 2, '11': 515}\n"
]
}
],
"execution_count": 7
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T07:48:00.433048Z",
"start_time": "2026-12-01T07:47:43.862323Z"
}
},
"source": [
"# Getting a job result with a timeout argument specified\n",
"from c12_callisto_clients.qiskit.exceptions import C12SimJobError\n",
"c12_job = c12_simulator_backend.run(circuit)\n",
"\n",
"try:\n",
" c12_result = c12_job.result(timeout=15) # it will raise a C12SimJobError as it won't be finished in 15s\n",
"except C12SimJobError:\n",
" print(\"Timeout!\")\n",
" print(f\" Last status: {c12_job.status()}\")"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Timeout!\n",
" Last status: JobStatus.RUNNING\n"
]
}
],
"execution_count": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Getting previous jobs\n",
"\n",
"Next, an extremely useful functionality is the possibility to obtain all jobs run on the system by a specific user. Using this functionality, the user can get the results of old jobs.\n",
"\n",
"This can be achieved by calling the `jobs()` function on the backend instance. This function accepts two arguments: `limit` and `offset`. We can specify how many jobs we want to get with a `limit` argument. Likewise, we can set the `offset` from the first job with an offset argument. With these arguments, pagination is easily achieved."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T08:27:51.607432Z",
"start_time": "2026-12-01T08:27:49.999446Z"
}
},
"source": [
"number_of_records = 5\n",
"offset = 10\n",
"counter = 1\n",
"\n",
"jobs = c12_simulator_backend.jobs(number_of_records, offset)\n",
"\n",
"for job in jobs:\n",
" print(f\"{counter}: {job.job_id()} Status: {job.status()}\")\n",
" counter += 1\n",
"\n"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1: 4dfd4440-38f0-48e5-b3be-d1562d98e5a7 Status: JobStatus.DONE\n",
"2: 64054207-62b4-48be-a017-47a54febc62a Status: JobStatus.DONE\n",
"3: 52884029-d337-41c9-9434-24e5aebb45a1 Status: JobStatus.DONE\n",
"4: 32712b40-269b-418b-a647-5f4b43240c62 Status: JobStatus.DONE\n",
"5: 9aad19ba-d311-4768-8d32-13a455979db3 Status: JobStatus.DONE\n"
]
}
],
"execution_count": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For each job, it is possible to get the circuit sent to the simulator for execution. But, it is even possible to get the transpiled version of this circuit to see what the circuit has been run after the transpilation process."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T08:28:10.817286Z",
"start_time": "2026-12-01T08:28:10.783852Z"
}
},
"source": [
"# Also it is possible to retrieve the circuit that has been run for specific job\n",
"# Getting the transpiled circuit\n",
"first_job = jobs[0]\n",
"first_job_qasm = first_job.get_qasm() # Get OpenQASM string\n",
"first_job_circuit = first_job.get_circuit(transpiled=False)\n",
"print(first_job_circuit)\n",
"\n",
"first_job_circuit_transpiled = first_job.get_circuit(transpiled=True)\n",
"print(first_job_circuit_transpiled)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌───┐ \n",
"q_0: ┤ H ├──■──\n",
" └───┘┌─┴─┐\n",
"q_1: ─────┤ X ├\n",
" └───┘\n",
" ┌────────┐ ┌───────┐ ┌────────┐┌──────────┐┌──────────┐\n",
"q_0: ────────────┤0 ├─┤ Rx(π) ├──┤0 ├┤ Ry(-π/2) ├┤ Rz(-π/2) ├\n",
" ┌──────────┐│ Iswap │┌┴───────┴─┐│ Iswap │├─────────┬┘├──────────┤\n",
"q_1: ┤ Ry(-π/2) ├┤1 ├┤ Ry(-π/2) ├┤1 ├┤ Rz(π/2) ├─┤ Ry(-π/2) ├\n",
" └──────────┘└────────┘└──────────┘└────────┘└─────────┘ └──────────┘\n"
]
}
],
"execution_count": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query job by UUID\n",
"\n",
"Jobs can be searched by their UUID using the `get_job()` method."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T07:52:50.883405Z",
"start_time": "2026-12-01T07:52:50.556542Z"
}
},
"source": [
"# Jobs can be queried by theirs UUID\n",
"job_uuid = first_job.job_id()\n",
"search_job = c12_simulator_backend.get_job(job_uuid)\n",
"\n",
"print(f\"Job id : {search_job.job_id()} {search_job.status()}\")"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Job id : 1e8542b1-a9d2-448a-8dd9-c959ead6772e JobStatus.DONE\n"
]
}
],
"execution_count": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Running multiple jobs at once\n",
"\n",
"The `run()` method of the backend class can also accept the list of QuantumCircuit. This can be an essential feature as it allows a user to run multiple circuits simultaneously and get an array of job instances as a result."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2026-12-01T07:54:45.673262Z",
"start_time": "2026-12-01T07:53:00.087434Z"
}
},
"source": [
"# Array where the circuits will be stored\n",
"circuits_to_run = []\n",
"\n",
"# Creating the circuits\n",
"circuit_1 = QuantumCircuit(2)\n",
"circuit_1.h(0)\n",
"circuit_1.cx(0, 1)\n",
"\n",
"circuit_2 = QuantumCircuit(3)\n",
"circuit_2.h(0)\n",
"circuit_2.cx(0, 1)\n",
"circuit_2.x(2)\n",
"circuit_2.h(2)\n",
"\n",
"circuits_to_run.append(circuit_1)\n",
"circuits_to_run.append(circuit_2)\n",
"\n",
"# Running the jobs\n",
"c12_jobs = c12_simulator_backend.run(circuits_to_run)\n",
"\n",
"# Printing the UUID of the jobs\n",
"for job in c12_jobs:\n",
" print(f\"Job id: {job.job_id()} - > {job.status()}\")\n",
"\n",
"# Waiting for the results\n",
"for job in c12_jobs:\n",
" result = job.result()\n",
" print(result.get_statevector())"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Job id: ae2b56b8-fecb-4015-8a35-cf47127a308e - > JobStatus.RUNNING\n",
"Job id: b4ebd7cc-750f-46d4-8ca2-3b1310bf9456 - > JobStatus.QUEUED\n",
"[-7.07350024e-01+5.77469795e-05j 5.75368906e-05+2.69994702e-04j\n",
" 5.74994602e-05+2.15881815e-04j -7.06863360e-01-5.72895433e-05j]\n",
"[ 5.00172138e-01-4.08332922e-05j -4.06847368e-05-1.90915138e-04j\n",
" -4.06582696e-05-1.52651538e-04j 4.99828015e-01+4.05098358e-05j\n",
" -5.00171860e-01+4.08332694e-05j 4.06847142e-05+1.90915032e-04j\n",
" 4.06582469e-05+1.52651453e-04j -4.99827736e-01-4.05098132e-05j]\n"
]
}
],
"execution_count": 24
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}